<template>
  <div class="banner-management">
    <el-card>
      <div class="filter-container">
        <el-button type="primary" @click="handleCreate">新增轮播图</el-button>
      </div>

      <el-table :data="list" border>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="标题" width="120" />
        <el-table-column label="图片">
          <template #default="{row}">
            <img :src="row.image_url" style="width: 100px; height: 60px" />
          </template>
        </el-table-column>
        <el-table-column prop="link" label="跳转链接" />
        <el-table-column prop="sort" label="排序" width="80" />
        <el-table-column label="状态">
          <template #default="{row}">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{row}">
            <el-button size="mini" @click="handleEdit(row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="图片" prop="image_url">
          <el-upload
            action="/api/banners/upload"
            list-type="picture-card"
            :on-success="handleUploadSuccess"
            :file-list="fileList"
            name="file" 
            :headers="uploadHeaders"
            :before-upload="beforeUpload">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="跳转链接" prop="link">
          <el-input v-model="form.link" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="1" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchBannerList, createBanner, updateBanner, deleteBanner } from '@/api/banner'

export default {
  data() {
    return {
      list: [],
      listQuery: {
        page: 1,
        limit: 10
      },
      dialogVisible: false,
      dialogTitle: '',
      isCreate: false,
      form: {
        id: '',
        title: '',
        image_url: '',
        link: '',
        sort: 1,
        status: 1
      },
      fileList: [],
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        image_url: [{ required: true, message: '请上传图片', trigger: 'blur' }],
        link: [{ required: true, message: '请输入跳转链接', trigger: 'blur' }]
      },
      uploadHeaders: {
        Authorization: `Bearer ${localStorage.getItem('token')}`
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      fetchBannerList(this.listQuery).then(response => {
        this.list = response.data.items
      })
    },
    handleCreate() {
      this.dialogTitle = '新增轮播图'
      this.isCreate = true
      this.form = {
        id: '',
        title: '',
        image_url: '',
        link: '',
        sort: 1,
        status: 1
      }
      this.fileList = []
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑轮播图'
      this.isCreate = false
      this.form = Object.assign({}, row)
      this.fileList = row.image_url ? [{ url: row.image_url }] : []
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该轮播图?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBanner(row.id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    handleUploadSuccess(response) {
      this.form.image_url = response.data.url
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const api = this.isCreate ? createBanner : updateBanner
          api(this.form).then(() => {
            this.$message.success('操作成功')
            this.dialogVisible = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>